﻿using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace Net5获取网卡信息
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string,string> allNet = new Dictionary<string, string>();
            allNet.Add("Default Network",null);

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                var type = adapter.GetType();
                var collection = type.GetProperties();
                
                if (adapter.OperationalStatus == OperationalStatus.Up)
                {

                    //if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
                    //adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                    {
                        IPInterfaceProperties ip = adapter.GetIPProperties();
                        UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                        foreach (UnicastIPAddressInformation ipadd in ipCollection)
                        {

                            if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                if (ipadd.Address.ToString() != "0.0.0.0")
                                {
                                    allNet.Add("(" + ipadd.Address.ToString() + ")" + adapter.Description, ipadd.Address.ToString());
                                }
                            }
                        }
                    }
                }
            }

            foreach (var item in allNet)
            {
                Console.WriteLine(item);
            }
        }
        
    }
}
